package com.apes.autoparts.searcher.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.searcher.repository.AutopartsDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("epcPartDescriptionService")
public class EpcPartDescriptionService extends DomainService {
    @Autowired
    AutopartsDao autopartsDao;

    public Map findAll(SimpleRequest request) {
        JSONObject param = request.getJO();
        JSONArray array = autopartsDao.searchPartDescription(param);

        return MapUtil.mapper(
                "total", array.size(),
                "pages", 1,
                "size", array.size(),
                "page", 0,
                "size", 300,
                "content", array.stream().map(json -> {
                    JSONObject tempJson = (JSONObject) json;
                    return MapUtil.mapper(
                            "id", tempJson.getString("ID"),
                            "name", tempJson.getString("NAME"),
                            "matchProductSign", tempJson.getIntValue("MATCHPRODUCTSIGN")
                    );
                }).toArray()
        );
    }

}
